package com.begye.smartloan.apptest.renzheng.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.begye.smartloan.apptest.R
import com.begye.smartloan.apptest.databinding.DiaFillItemBangKaSubmitBinding
import com.begye.smartloan.apptest.renzheng.adapter.FillItemBangKaSubmitDialogAdapter
import com.begye.smartloan.apptest.tool.addTouch
import org.json.JSONObject

class FillItemBangKaSubmitDialogFragment : DialogFragment() {

    private var _binding: DiaFillItemBangKaSubmitBinding? = null
    private val binding get() = _binding!!
    private var _clickListener: () -> Unit = { }

    private lateinit var _fillItemBangKaSubmitDialogAdapter: FillItemBangKaSubmitDialogAdapter

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val layout = inflater.inflate(R.layout.dia_fill_item_bang_ka_submit, container, false)
        _binding = DiaFillItemBangKaSubmitBinding.bind(layout)
        return layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Theme_Bottom_Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        _fillItemBangKaSubmitDialogAdapter = FillItemBangKaSubmitDialogAdapter(emptyList())
        binding.diaFillItemBangKaSubmitList.layoutManager = LinearLayoutManager(context)
        binding.diaFillItemBangKaSubmitList.adapter = _fillItemBangKaSubmitDialogAdapter

        binding.diaFillItemBangKaSubmitClose.setOnClickListener {
            dismiss()
        }

        binding.diaFillItemBangKaSubmitYes.setOnClickListener {
            _clickListener.invoke()

            //
            addTouch(type = "bang_ka_submit_dialog_commit_click")

            dismiss()
        }
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return

        window.attributes.gravity = Gravity.CENTER
        window.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )

        _fillItemBangKaSubmitDialogAdapter.notifyDataSetChanged()

        //
        addTouch(type = "bang_ka_submit_dialog_show")
    }

    fun addDataSource(value: List<JSONObject>): FillItemBangKaSubmitDialogFragment {
        try {
            _fillItemBangKaSubmitDialogAdapter.dataList = value
        } catch (e: Exception) {
            //
        }
        return this
    }

    fun addClickListener(listener: () -> Unit): FillItemBangKaSubmitDialogFragment {
        _clickListener = listener
        return this
    }
}